package com.kede.app.rest;

import com.kede.annotation.AnonymousAccess;
import com.kede.annotation.Log;
import com.kede.app.domain.Operat;
import com.kede.app.service.OperatService;
import com.kede.app.service.dto.OperatQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author Pr.li
* @date 2023-08-13
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "operat管理")
@RequestMapping("/api/operat")
public class OperatController {

    private final OperatService operatService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('operat:list')")
    public void exportOperat(HttpServletResponse response, OperatQueryCriteria criteria) throws IOException {
        operatService.download(operatService.queryAll(criteria), response);
    }

    @AnonymousAccess
    @GetMapping(value = "/downLoadImg")
    public void downLoadImg(HttpServletRequest request, HttpServletResponse response, String senceno) throws IOException {
        operatService.downLoadImg( request ,response, senceno);
    }

    /**
    * 查询operat
    **/
    @GetMapping
    @Log("查询operat")
    @ApiOperation("查询operat")
    @PreAuthorize("@el.check('operat:list')")
    public ResponseEntity<Object> queryOperat(OperatQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(operatService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ApiOperation("生成推官人员小程序二维码")
    @PostMapping("buildOwnQCode")
    @PreAuthorize("@el.check('operat:build')")
    public ResponseEntity buildOwnQCode(@RequestBody Operat operat){
        return operatService.buildOwnQCode(operat);
    }



    /**
    * 新增operat
    **/
    @PostMapping
    @Log("新增operat")
    @ApiOperation("新增operat")
    @PreAuthorize("@el.check('operat:add')")
    public ResponseEntity<Object> createOperat(@Validated @RequestBody Operat resources){
        return new ResponseEntity<>(operatService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改operat
    **/
    @PutMapping
    @Log("修改operat")
    @ApiOperation("修改operat")
    @PreAuthorize("@el.check('operat:edit')")
    public ResponseEntity<Object> updateOperat(@Validated @RequestBody Operat resources){
        operatService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除operat
    **/
    @DeleteMapping
    @Log("删除operat")
    @ApiOperation("删除operat")
    @PreAuthorize("@el.check('operat:del')")
    public ResponseEntity<Object> deleteOperat(@RequestBody Integer[] ids) {
        operatService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}